import router from './router'

const store = {
    state: {
        permission: [],
        tag_id: '0'
    },
    setPer(newVal) {
        this.state.permission = newVal;
    },
    getPer(...apilist) {
        return (
            apilist.filter((route) => {
                return this.state.permission.includes(route);
            }).length > 0
        );
    },
    getTagPer() {
        return +this.state.tag_id;
    },
    setTagPer(newVal) {
        this.state.tag_id = newVal || '0';
    }
};

/**
 * 路由的权限控制
 */
router.beforeEach((to, from, next) => {
    if (to.name !== 'Login' && !localStorage.getItem('TOKEN')) {
        // 登录权限跳转
        router.replace({ name: 'Login' });
    } else if (from.name && to.meta.auth && !store.getPer(to.meta.auth)) {
        // 权限控制的跳转
        router.replace({ name: 'Index' });
    } else {
        next();
    }
});